/**
 * @file dumper.cpp
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2020-10-29
 *
 * @copyright Copyright (c) 2020
 *
 */
#include "dumper.h"
#include "lib/TECIO.h"
/**
 * @brief set tecplot header
 *
 * @param file_name
 * @param var_name
 * @param zone_name
 * @param sizes                 [0:3] = [Imax,Jmax,Kmax,number of varaibles]
 * @param num_node_variables
 * @param solution_time
 */
void Dumper::DumpTecplotHeaderBinary(string file_name, string var_name,
                                     string zone_name, size_t *sizes,
                                     size_t num_node_variables,
                                     double solution_time) {
#if (USE_DEBUG)
  // x y z should be output via nodes
  assert(num_node_variables > 0);
  assert(num_node_variables < 4);
#endif
  INTEGER4 Debug = 0;
  INTEGER4 VIsDouble = 0;
  INTEGER4 FileType = 0;
  INTEGER4 FileFormat = 0;
  INTEGER4 I = 0;                  // Used to track return codes
  I = TECINI112(zone_name.c_str(), /* Name of the entire
                                    * dataset.
                                    */
                var_name.c_str(),  /* Defines the variables for the data
                                    * file. Each zone must contain each of
                                    * the variables listed here. The order
                                    * of the variables in the list is used
                                    * to define the variable number (e.g.
                                    * X is Var 1).
                                    */
                file_name.c_str(), (char *)".", /* Scratch Directory */
                &FileType, &Debug, &VIsDouble);

  INTEGER4 IMax = sizes[0];
  INTEGER4 JMax = sizes[1];
  INTEGER4 KMax = sizes[2];
  INTEGER4 ICellMax = 0;
  INTEGER4 JCellMax = 0;
  INTEGER4 KCellMax = 0;
  INTEGER4 DIsDouble = 0;
  double SolTime = solution_time;
  INTEGER4 StrandID = 1; /* StaticZone */
  INTEGER4 ParentZn = 0;
  INTEGER4 IsBlock = 1; /* Block */
  INTEGER4 NFConns = 0;
  INTEGER4 FNMode = 0;
  INTEGER4 TotalNumFaceNodes = 1;
  INTEGER4 TotalNumBndryFaces = 1;
  INTEGER4 TotalNumBndryConnections = 1;
  unique_ptr<INTEGER4[]> valueLocations = make_unique<INTEGER4[]>(sizes[3]);
  INTEGER4 ShrConn = 0;

  for (int i = 0; i < num_node_variables; ++i) {
    valueLocations[i] = 1;
  }

  //  Create the ordered Zone
  INTEGER4 ZoneType = 0;
  I = TECZNE112(zone_name.c_str(), &ZoneType, &IMax, &JMax, &KMax, &ICellMax,
                &JCellMax, &KCellMax, &SolTime, &StrandID, &ParentZn, &IsBlock,
                &NFConns, &FNMode, &TotalNumFaceNodes, &TotalNumBndryFaces,
                &TotalNumBndryConnections,
                NULL,                 // PassiveVarList
                valueLocations.get(), // ValueLocation
                NULL,                 // ShareVarFromZone
                &ShrConn);
}
/**
 * @brief set tecplot header
 *
 * @param file_name
 * @param var_name
 * @param zone_name
 * @param sizes                 [0:3] = [Imax,Jmax,Kmax,number of varaibles]
 * @param num_node_variables
 * @param solution_time
 */
void Dumper::DumpTecplotHeaderASCII(string file_name, string var_name,
                                    string zone_name, size_t *sizes,
                                    size_t num_node_variables,
                                    double solution_time) {
#if (USE_DEBUG)
  // x y z should be output via nodes
  assert(num_node_variables > 0);
  assert(num_node_variables < 4);
#endif
  ofstream out(file_name.c_str());
  // tecplot file header
  out << "\n";
  out << "title='View'"
      << "\n";
  out << "variables=" << var_name << endl;

  out << "zone t=" << zone_name.c_str() << ", i= " << sizes[0]
      << ", j= " << sizes[1] << ", k= " << sizes[2]
      << "  DATAPACKING=BLOCK, VARLOCATION=([";
  out << num_node_variables + 1;
  if (sizes[3] - num_node_variables > 1) {
    out << "-";
    out << sizes[3];
  }
  out << "]=CELLCENTERED) SOLUTIONTIME=" << solution_time << "\n";
  out.close();
}
/**
 * @brief output file
 *
 * @param var
 * @param num
 * @param solution_time
 */
void Dumper::DumpTecplotVariablesBinary(string file_name, float *var,
                                        size_t num) {
  INTEGER4 num_tmp = num;
  INTEGER4 I = 0;
  INTEGER4 DIsDouble = 0;
  I = TECDAT112(&num_tmp, var, &DIsDouble);
}
/**
 * @brief outputfile
 *
 * @param file_name
 * @param var
 * @param num
 * @param solution_time
 */
void Dumper::DumpTecplotVariablesASCII(string file_name, float *var,
                                       size_t num) {
  const size_t interval = 16;
  ofstream out(file_name.c_str(), ios::app);
  for (size_t i = 0; i < num; ++i) {
    out << var[i] << " ";
    if (i % interval == 0)
      out << endl;
  }
  out << endl;
  out.close();
}
/**
 * @brief
 *
 */
void Dumper::DumpTecplotTailBinary() {
  INTEGER4 I = 0;
  I = TECEND112();
}